/*
 * @Author: 生俊甫 1758142861@qq.com
 * @Date: 2025-01-19 17:29:57
 * @LastEditors: 生俊甫 1758142861@qq.com
 * @LastEditTime: 2025-01-25 20:07:09
 * @FilePath: /sjf/weChat_project/test_to_there/framework/etcd_test/client.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <etcd/Client.hpp>
#include <etcd/KeepAlive.hpp>
#include <etcd/Response.hpp>
#include <thread>
#include "../spdlog_test/logger.hpp"

/**
 * 主要的就是
 * 1.实例化客户端
 * 2.获取租约保活对象
 * 3.获取租约ID
 * 4.向etcd新增数据
 */
DEFINE_bool(run_mode,false,"运行模式(true表示发布模式,false表示调试模式)");
DEFINE_string(log_file,"","发布模式下,指定的日志输出文件名");
DEFINE_int32(log_level,0,"发布模式下,指定的日志输出等级");

int main(int argc,char* argv[])
{
    //
    google::ParseCommandLineFlags(&argc,&argv,true);
    init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);
    std::string etcd_host = "http://127.0.0.1:2379";
    //实例化客户端对象
    etcd::Client client(etcd_host);
    //获取租约保活对象
    auto keep_alive = client.leasekeepalive(3).get();
    //获取租约ID
    auto lease_id = keep_alive->Lease();
    //向etcd请求新增数据
    /**
     * pplx::task<Response> put(std::string const& key,  
        std::string const& value, 
        const int64_t leaseId); 
     */
    std::string client_key = "/service/user";
    std::string client_val = "127.0.0.1:8080";
    // auto resp = client.put("/service/user","127.0.0.1:8080",lease_id).get();
    auto resp = client.put(client_key,client_val,lease_id).get();
    if(resp.is_ok() == false)
    {
        LOG_ERROR("新增数据失败: {}",resp.error_message());
        return -1;
    }
    std::this_thread::sleep_for(std::chrono::seconds(10));
    return 0;
}